package com.taoyibao.hongcanting.ui.mine.delegate;

import android.net.Uri;
import android.support.v4.app.FragmentManager;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.taoyibao.hongcanting.R;
import com.taoyibao.hongcanting.base.delegate.BackToolbarDelegate;
import com.taoyibao.hongcanting.constant.CodeConstant;
import com.taoyibao.hongcanting.dialog.DailogChooseTime;
import com.taoyibao.hongcanting.dialog.DialogChooseEthnic;
import com.taoyibao.hongcanting.dialog.DialogChooseGender;
import com.taoyibao.hongcanting.model.UserInfoModel;
import com.taoyibao.hongcanting.model.UserVerifyModel;
import com.taoyibao.hongcanting.utils.CodeUtils;
import com.taoyibao.hongcanting.utils.GlideUtil;
import com.taoyibao.hongcanting.utils.LogUtils;
import com.taoyibao.hongcanting.utils.ToastUtils;

/**
 * @author TangAnna
 * @description: 实名认证
 * @date :${DATA} 13:22
 */
public class VerifiedDelegate extends BackToolbarDelegate implements View.OnClickListener, DialogChooseGender.DialogGrnderInterface, DialogChooseEthnic.DialogEthnicInterface, DailogChooseTime.DialogChooseTime {

    private FragmentManager mFragmentManager;
    private Uri mPhoneZheng;//身份证证明照片
    private Uri mPhoneFan;//身份证反面照片
    private Uri mPhonePerson;//手持身份证的照片
    private TextView mTvStatus;
    private ImageView mIvZheng;//身份证正面
    private ImageView mIvFan;//身份证反面
    private ImageView mIvPerson;//手持身份证

    private EditText mTvName;//姓名
    private TextView mTvGender;//性别  1男  2女
    private TextView mTvEthnic;//民族
    private TextView mTvBirthday;//出生年月
    private EditText mTvAddress;//住址
    private EditText mTvIdCard;//身份证号


    @Override
    public int getRootLayoutId() {
        return R.layout.activity_verified;
    }

    @Override
    public void initWidget() {
        super.initWidget();
        setTitle("实名认证");
        setLoadingView();
        mIvZheng = get(R.id.iv_verified_idCardZheng);
        mIvFan = get(R.id.iv_verified_idCardFan);
        mIvPerson = get(R.id.iv_verified_person);
        mTvName = get(R.id.et_veridied_name);
        mTvGender = get(R.id.tv_verified_gender);
        mTvEthnic = get(R.id.tv_verified_ethnic);
        mTvBirthday = get(R.id.tv_verified_birthday);
        mTvAddress = get(R.id.et_verified_address);
        mTvIdCard = get(R.id.et_verified_idCard);

        get(R.id.layout_verified_gender).setOnClickListener(this);
        get(R.id.layout_verified_ethnic).setOnClickListener(this);
        get(R.id.layout_verified_birthday).setOnClickListener(this);
        get(R.id.tv_verified_reApply).setOnClickListener(this);

        mTvStatus = get(R.id.tv_verified_status);
        get(R.id.view_verified_notClickable).setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return true;
            }
        });

    }

    public void setFragmentManager(FragmentManager mFragmentManager) {
        this.mFragmentManager = mFragmentManager;
    }

    public void setData(UserInfoModel userInfoModel) {
        setDataView();
        if (userInfoModel == null || userInfoModel.user_verify == null || TextUtils.isEmpty(userInfoModel.user_verify.memberId)) {
            return;
        }
        //实名认证信息
        UserVerifyModel userVerify = userInfoModel.user_verify;

        switch (userVerify.status) {
            case CodeConstant.VERIFY_WAIT://待审核
                get(R.id.view_verified_notClickable).setVisibility(View.VISIBLE);
                get(R.id.layout_verified_status).setVisibility(View.VISIBLE);
                get(R.id.lauout_verified_cause).setVisibility(View.GONE);
                get(R.id.btn_verified_confirm).setVisibility(View.GONE);
                mTvStatus.setText(CodeUtils.getIdString(R.string.status_wait));
                mTvStatus.setTextColor(CodeUtils.getIdColor(R.color.color5ec));
                break;
            case CodeConstant.VERIFY_PARSS://成功
                get(R.id.view_verified_notClickable).setVisibility(View.VISIBLE);
                get(R.id.layout_verified_status).setVisibility(View.VISIBLE);
                get(R.id.lauout_verified_cause).setVisibility(View.GONE);
                get(R.id.btn_verified_confirm).setVisibility(View.GONE);
                mTvStatus.setText(CodeUtils.getIdString(R.string.status_finish));
                mTvStatus.setTextColor(CodeUtils.getIdColor(R.color.color999));
                break;
            case CodeConstant.VERIFY_FAIL://失败
                get(R.id.view_verified_notClickable).setVisibility(View.VISIBLE);
                get(R.id.layout_verified_status).setVisibility(View.VISIBLE);
                get(R.id.lauout_verified_cause).setVisibility(View.VISIBLE);
                get(R.id.tv_verified_reApply).setVisibility(View.VISIBLE);
                get(R.id.btn_verified_confirm).setVisibility(View.GONE);
                mTvStatus.setText(CodeUtils.getIdString(R.string.status_failed));
                mTvStatus.setTextColor(CodeUtils.getIdColor(R.color.colorf84));
                //失败原因
                if (!TextUtils.isEmpty(userVerify.reason)) {
                    ((TextView) get(R.id.tv_verified_cause)).setText(userVerify.reason);
                }
                break;
            case CodeConstant.VERIFY_NOT://未认证
                get(R.id.view_verified_notClickable).setVisibility(View.GONE);
                get(R.id.lauout_verified_cause).setVisibility(View.GONE);
                get(R.id.layout_verified_status).setVisibility(View.GONE);
                get(R.id.tv_verified_reApply).setVisibility(View.GONE);
                get(R.id.btn_verified_confirm).setVisibility(View.VISIBLE);
                break;
            default://当做未认证处理
                get(R.id.view_verified_notClickable).setVisibility(View.GONE);
                get(R.id.lauout_verified_cause).setVisibility(View.GONE);
                get(R.id.layout_verified_status).setVisibility(View.GONE);
                get(R.id.tv_verified_reApply).setVisibility(View.GONE);
                get(R.id.btn_verified_confirm).setVisibility(View.VISIBLE);
                break;
        }
        //显示回显数据
        mIvZheng.setScaleType(ImageView.ScaleType.FIT_CENTER);
        mIvFan.setScaleType(ImageView.ScaleType.FIT_CENTER);
        mIvPerson.setScaleType(ImageView.ScaleType.FIT_CENTER);
        GlideUtil.getInstance().loadDefaultImage(getActivity(), userVerify.id_front_photo, mIvZheng);
        GlideUtil.getInstance().loadDefaultImage(getActivity(), userVerify.id_back_photo, mIvFan);
        GlideUtil.getInstance().loadDefaultImage(getActivity(), userVerify.id_people_photo, mIvPerson);
        //姓名
        if (!TextUtils.isEmpty(userVerify.user_name)) {
            mTvName.setText(userVerify.user_name);
        }
        //性别
        if (userVerify.gender == CodeConstant.MALE) {//男
            mTvGender.setText("男");
        } else if (userVerify.gender == CodeConstant.FEMALE) {//女
            mTvGender.setText("女");
        }

        //民族
        if (!TextUtils.isEmpty(userVerify.nation)) {
            mTvEthnic.setText(userVerify.nation);
        }
        //生日
        mTvBirthday.setText(CodeUtils.formatDate(userVerify.birth_date));

        //住址
        if (!TextUtils.isEmpty(userVerify.residence)) {
            mTvAddress.setText(userVerify.residence);
        }
        //身份证号
        if (!TextUtils.isEmpty(userVerify.id_no)) {
            mTvIdCard.setText(userVerify.id_no);
        }
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.layout_verified_gender://选择性别
                DialogChooseGender mDialogChooseGender = new DialogChooseGender(this);
                mDialogChooseGender.show(mFragmentManager, "TAG");
                break;
            case R.id.layout_verified_ethnic://选择性别
                DialogChooseEthnic mDialogChooseEthnic = new DialogChooseEthnic(this);
                mDialogChooseEthnic.show(mFragmentManager, "TAG");
                break;
            case R.id.layout_verified_birthday://选择时间
                DailogChooseTime dailogChooseTime = new DailogChooseTime(this);
                dailogChooseTime.show(mFragmentManager, "TAG");
                break;
            case R.id.tv_verified_reApply://重新申请
                get(R.id.view_verified_notClickable).setVisibility(View.GONE);
                get(R.id.lauout_verified_cause).setVisibility(View.GONE);
                get(R.id.layout_verified_status).setVisibility(View.GONE);
                get(R.id.tv_verified_reApply).setVisibility(View.GONE);
                get(R.id.btn_verified_confirm).setVisibility(View.VISIBLE);
                break;
        }
    }


    /**
     * 得到的性别
     *
     * @param grnder
     */
    @Override
    public void onGrnderResult(String grnder) {
        if (!TextUtils.isEmpty(grnder)) {
            ((TextView) get(R.id.tv_verified_gender)).setText(grnder);
        }
    }

    /**
     * 获取性别
     *
     * @return
     */
    public String getGrnder() {
        String grnder = ((TextView) get(R.id.tv_verified_gender)).getText().toString().trim();
        if (TextUtils.equals(grnder, "男")) {
            return CodeConstant.MALE + "";
        } else if (TextUtils.equals(grnder, "女")) {
            return CodeConstant.FEMALE + "";
        }
        ToastUtils.showToast(CodeUtils.getIdString(R.string.verified_hintGender));
        return "";
    }

    /**
     * 民族的回调
     *
     * @param ethnic
     */
    @Override
    public void onEthnicResult(String ethnic) {
        if (!TextUtils.isEmpty(ethnic)) {
            ((TextView) get(R.id.tv_verified_ethnic)).setText(ethnic);
        }
    }

    /**
     * 获取民族
     *
     * @return
     */
    public String getEthnic() {
        if (!TextUtils.isEmpty(((TextView) get(R.id.tv_verified_ethnic)).getText().toString().trim())) {
            return ((TextView) get(R.id.tv_verified_ethnic)).getText().toString().trim();
        }
        ToastUtils.showToast(CodeUtils.getIdString(R.string.verified_hintEthnic));
        return "";
    }

    /**
     * 获取用户输入的姓名
     *
     * @return
     */
    public String getName() {
        if (!TextUtils.isEmpty(((EditText) get(R.id.et_veridied_name)).getText().toString().trim())) {
            return ((EditText) get(R.id.et_veridied_name)).getText().toString().trim();
        }
        ToastUtils.showToast(CodeUtils.getIdString(R.string.verified_hintName));
        return "";
    }

    /**
     * 获取出生年月
     *
     * @return
     */
    public String getBirthday() {
        if (!TextUtils.isEmpty(((TextView) get(R.id.tv_verified_birthday)).getText().toString().trim())) {
            return ((TextView) get(R.id.tv_verified_birthday)).getText().toString().trim();
        }
        ToastUtils.showToast(CodeUtils.getIdString(R.string.verified_hintBirthday));
        return "";
    }

    /**
     * 获取出生年月
     *
     * @return
     */
    public String getLongBirthday() {
        if (!TextUtils.isEmpty(getBirthday())) {
            String birthday = getBirthday();
            LogUtils.dNormal("生日之前", birthday);
            LogUtils.dNormal("生日转换之后", CodeUtils.formatDate(birthday) + "");
            return CodeUtils.formatDate(birthday) + "";
        }
        return "";
    }

    /**
     * 获取住址
     *
     * @return
     */
    public String getAddress() {
        if (!TextUtils.isEmpty(((EditText) get(R.id.et_verified_address)).getText().toString().trim())) {
            return ((EditText) get(R.id.et_verified_address)).getText().toString().trim();
        }
        ToastUtils.showToast(CodeUtils.getIdString(R.string.verified_hintAddress));
        return "";
    }

    /**
     * 获取身份证号
     *
     * @return
     */
    public String getIDCard() {
        if (!TextUtils.isEmpty(((EditText) get(R.id.et_verified_idCard)).getText().toString().trim())) {
            return ((EditText) get(R.id.et_verified_idCard)).getText().toString().trim();
        }
        ToastUtils.showToast(CodeUtils.getIdString(R.string.verified_hintIDCard));
        return "";
    }

    /**
     * 获取身份证正面照片
     *
     * @return
     */
    public Uri getPhotoZheng() {
        if (mPhoneZheng != null) {
            return mPhoneZheng;
        }
        ToastUtils.showToast(CodeUtils.getIdString(R.string.dialog_IDCardZhengMian));
        return null;
    }

    /**
     * 获取身份证正面照片
     *
     * @return
     */
    public Uri getPhotoFan() {
        if (mPhoneFan != null) {
            return mPhoneFan;
        }
        ToastUtils.showToast(CodeUtils.getIdString(R.string.dialog_IDCardFanMian));
        return null;
    }

    /**
     * 获取身份证正面照片
     *
     * @return
     */
    public Uri getPhotoPerson() {
        if (mPhonePerson != null) {
            return mPhonePerson;
        }
        ToastUtils.showToast(CodeUtils.getIdString(R.string.dialog_IDCarAndPerson));
        return null;
    }

    public void setPhoneZheng(Uri phoneZheng) {
        mPhoneZheng = phoneZheng;
        mIvZheng.setScaleType(ImageView.ScaleType.FIT_CENTER);
        GlideUtil.getInstance().loadDefaultImage(getActivity(), phoneZheng.toString(), mIvZheng);
    }

    public void setPhoneFan(Uri phoneFan) {
        mPhoneFan = phoneFan;
        mIvFan.setScaleType(ImageView.ScaleType.FIT_CENTER);
        GlideUtil.getInstance().loadDefaultImage(getActivity(), phoneFan.toString(), mIvFan);
    }

    public void setPhonePerson(Uri phonePerson) {
        mPhonePerson = phonePerson;
        mIvPerson.setScaleType(ImageView.ScaleType.FIT_CENTER);
        GlideUtil.getInstance().loadDefaultImage(getActivity(), phonePerson.toString(), mIvPerson);
    }

    /**
     * 判断是否可以上传到服务器
     *
     * @return
     */
    public boolean canUpData() {
        if (!TextUtils.isEmpty(getName()) && !TextUtils.isEmpty(getGrnder()) && !TextUtils.isEmpty(getEthnic())
                && !TextUtils.isEmpty(getBirthday()) && !TextUtils.isEmpty(getAddress())
                && !TextUtils.isEmpty(getIDCard()) && getPhotoZheng() != null
                && getPhotoFan() != null && getPhotoPerson() != null) {
            return true;
        }
        return false;
    }

    /**
     * 时间回调
     *
     * @param year
     * @param month
     * @param day
     */
    @Override
    public void onTimeResult(String year, String month, String day) {
        if (!TextUtils.isEmpty(year) && !TextUtils.isEmpty(month) && !TextUtils.isEmpty(day)) {
            ((TextView) get(R.id.tv_verified_birthday)).setText(year + month + day);
        }
    }


}
